/*
# MIAHX© 框架消息的 FastDDS 订阅器（C++实现）

Version: 0.4.10,
Last updated: October 16, 2024

## Contributor(s)
* YAO Naiming <[naiming@iscas.ac.cn](mailto:naiming@iscas.ac.cn)>

## Change log
### v0.4.10
- .16: [更新] 新增注释
- .15: [更新] 版本同步至 Python 版v0.4.10.14
### v0.4.7
- .26: [新增] 初始实现

---
Copyright © 2024-2025 MIAHX Contributors, ISCAS, and ASIC. All Rights Reserved.
Licensed under Apache-2.0.
*/

#include "../include/miahx/reader.h"


ReaderListener::ReaderListener(
        TopicDataType *topic_data_type, data_listener &on_data_listener, void *user_state)
        : mTopicDataType(topic_data_type), mOnDataListener(on_data_listener), mUserState(user_state) {

}

ReaderListener::~ReaderListener() {
    delete mTopicDataType;
}

void ReaderListener::on_data_available(DataReader *reader) {
    // 处理接收到的订阅数据
}

void ReaderListener::on_subscription_matched(DataReader *reader,
                                             const SubscriptionMatchedStatus &info) {
    if (0 < info.current_count_change) {
        std::cout << "Subscriber matched publisher " << info.last_publication_handle << std::endl;
    } else {
        std::cout << "Subscriber unmatched publisher " << info.last_publication_handle << std::endl;
    }
}


Reader::Reader(Participant *participant, const string &topic_name,
               TopicDataType *topic_data_type, DataReaderListener *listener) {
    assert(nullptr != participant);
    assert(!topic_name.empty());

    this->topic_name = topic_name;

    auto topic = participant->add_topic(topic_name, topic_data_type);
    assert(nullptr != topic);
    mSubscriber = participant->add_subscriber(SubscriberQosTypes::SUB_QOS_TYPES_DEFAULT);
    DataReaderQos r_qos;
    mSubscriber->get_default_datareader_qos(r_qos);
    mReader = mSubscriber->create_datareader(topic, r_qos, listener);
}

Reader::~Reader() {
    if (nullptr != mSubscriber) {
        if (nullptr != mReader) {
            mSubscriber->delete_datareader(mReader);
        }
    }
}

void Reader::wait(unique_lock<mutex> &lock, condition_variable &cond) {
    cond.wait(lock);
}
